package cn.itsource.basic.util;

import cn.itsource.basic.jwt.JwtUtils;
import cn.itsource.basic.jwt.LoginData;
import cn.itsource.basic.jwt.Payload;
import cn.itsource.basic.jwt.RsaUtils;
import cn.itsource.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * 获取当前登录人信息
 */
public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        try {
            //获取公钥
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
            //解密
            Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
            return payload.getLoginData().getLogininfo();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
